<template>
  <div class="app-container">
    <SearchForm
      v-show="showSearch"
      v-model="queryParams"
      :options="options.formOps"
      @onQuery="getList"
    >
      <template #default="{ filterForm, handleQuery }"> </template>
    </SearchForm>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['danger:chemical:query:detailed:add']"
          >新增</el-button
        >
      </el-col>

      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <Table
      ref="table"
      :auto-featch="true"
      :fetch-data="fetchData"
      :options="options.table"
    >
      <template #default>
        <el-table-column
          label="所属部门"
          align="center"
          key="department"
          prop="department"
        />
        <el-table-column
          label="创建人"
          align="center"
          key="createdBy"
          prop="createdBy"
        />
        <el-table-column
          label="创建时间"
          align="center"
          key="createdDate"
          prop="createdDate"
          width="200"
        />
        <el-table-column
          label="更新时间"
          align="center"
          key="modifyDate"
          prop="modifyDate"
          width="200"
        />
        <el-table-column
          label="安全技术说明书"
          align="center"
          key="updateTime"
          prop="updateTime"
          width="200"
        >
          <template #default="{ row }">
            <el-button
              :disabled="!row.explainFileUrl"
              type="text"
              icon="download"
              @click="handleDownload(row, 'explainFile')"
              >下载</el-button
            >
          </template>
        </el-table-column>
        <el-table-column
          label="安全标签附件"
          align="center"
          key="updateTime"
          prop="updateTime"
          width="200"
        >
          <template #default="{ row }">
            <el-button
              :disabled="!row.lableFileUrl"
              type="text"
              icon="download"
              @click="handleDownload(row)"
              >下载</el-button
            >
          </template>
        </el-table-column>
      </template>
      <template #actionButton="{ row }">
        <el-button
          type="text"
          icon="Edit"
          @click="handleEdit(row)"
          v-hasPermi="['danger:chemical:query:detailed:update']"
          >修改</el-button
        >

        <el-button
          type="text"
          icon="Delete"
          style="color: red"
          @click="handleDel(row)"
          v-hasPermi="['danger:chemical:query:detailed:delete']"
          >删除</el-button
        >
      </template></Table
    >
    <DialogVform
      :formJson="formJson"
      ref="dialogVform"
      @onSubmit="onSubmit"
      :slots="['fileSlot', 'deptSlot']"
      :title="dialogType == 'add' ? '新增化学品信息' : '修改化学品信息'"
    >
      <template #deptSlot>
        <el-form-item label="所属部门">
          <!-- <el-input v-model="form.department" disabled></el-input> -->
          <TreeSelect
            v-model="form.department"
            :options="deptTree"
            @change="changeDept"
            :objMap="treeOption"
          ></TreeSelect>
        </el-form-item>
      </template>
      <template #fileSlot>
        <el-form-item label="安全技术说明书">
          <FileUpload
            :limit="1"
            :fileSize="30"
            :fileType="['doc', 'docx', 'pdf']"
            v-model="explainFile"
          ></FileUpload>
        </el-form-item>
        <el-form-item label="安全标签">
          <FileUpload
            :limit="1"
            :fileSize="30"
            :fileType="['doc', 'docx', 'pdf']"
            v-model="labelFile"
          ></FileUpload>
        </el-form-item>
      </template>
    </DialogVform>
  </div>
</template>

<script setup>
import Table from "@/components/Table/index.vue";
import SearchForm from "@/components/searchForm/index";
import DialogVform from "@/components/dialogVform/index";
import FileUpload from "@/components/FileUpload";
import { apis as APIS } from "@/api/hxp/wxhxpqd.js";
import formJson from "./components/options.json";
import { getUserProfile } from "@/api/system/user";
import { useAsyncData, useFormJson } from "@/views/hooks/useAsyncData.js";
import TreeSelect from "@/components/TreeSelect";
import { deptTreeSelect } from "@/api/system/user";

const { proxy } = getCurrentInstance();
const treeOption = {
  value: "label",
  label: "label",
  children: "children",
};
const ops = useFormJson(formJson, {
  sort: [],
  sortable: [],
});
const options = reactive({
  table: ops,
  formOps: ops.filter((i) =>
    ["productName", "addressInfo", "monitorCode", "registerCode"].includes(
      i.prop
    )
  ),
});
const labelFile = ref([]);
const explainFile = ref([]);
watch(
  () => labelFile,
  (newData) => {
    if (newData.value.length === 0) {
      form.value.lableFileName = "";
      form.value.lableFileId = "";
      form.value.lableFileUrl = "";
    } else {
      form.value.lableFileName = newData.value[0].name;
      form.value.lableFileId = newData.value[0].id;
      form.value.lableFileUrl = newData.value[0].url;
    }
  },
  {
    deep: true,
  }
);
watch(
  () => explainFile,
  (newData) => {
    if (newData.value.length === 0) {
      form.value.explainFileName = "";
      form.value.explainFileId = "";
      form.value.explainFileUrl = "";
    } else {
      form.value.explainFileName = newData.value[0].name;
      form.value.explainFileId = newData.value[0].id;
      form.value.explainFileUrl = newData.value[0].url;
    }
  },
  {
    deep: true,
  }
);

const handleDownload = (row, type) => {
  if (type) {
    window.downLoadFile(row.explainFileUrl, row.explainFileName);
  } else {
    window.downLoadFile(row.lableFileUrl, row.lableFileName);
  }
};
const showSearch = ref(true);

const deptName = ref("");
const plugAdd = () => {
  labelFile.value = [];
  explainFile.value = [];
  editRow.value = {};
  form.value.department = deptName.value;
};
const editRow = ref({});
const plugEdit = (row) => {
  editRow.value = { ...row };
  form.value.department = row.department;
  if (row.lableFileId) {
    labelFile.value = [
      {
        name: row.lableFileName,
        id: row.lableFileId,
        url: row.lableFileUrl,
      },
    ];
  } else {
    explainFile.value = [];
  }
  if (row.explainFileId) {
    explainFile.value = [
      {
        name: row.explainFileName,
        id: row.explainFileId,
        url: row.explainFileUrl,
      },
    ];
  } else {
    labelFile.value = [];
  }
};
const table = ref();
const dialogVform = ref();
const {
  onSubmit,
  fetchData,
  dialogType,
  handleAdd,
  handleEdit,
  getList,
  handleDel,
  queryParams,
  form,
} = useAsyncData({
  APIS,
  formJson,
  dialogVform,
  table,
  proxy,
  plugAdd,
  plugEdit,
});

const deptTree = ref([]);
onMounted(() => {
  deptTreeSelect().then((res) => {
    deptTree.value = res.data;
  });
});
onMounted(() => {
  deptTreeSelect().then((res) => {
    deptTree.value = res.data;
  });
  let userInfo = localStorage.getItem("USER_INFO");
  if (userInfo) {
    deptName.value = JSON.parse(userInfo).dept.deptName;
    console.log(form.value);
  } else {
    getUserProfile().then((res) => {
      deptName.value = res.data.dept.deptName;
    });
  }
});
const changeDept = (val, label) => {
  form.value.department = label;
  form.value.deptId = val;
};
</script>

<style lang="scss" scoped></style>
